<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 客户信息 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-lx-add" @click="handleEdit('-1', {})">添加</el-button>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>

                <el-input v-model="query.name" placeholder="客户名称" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="客户编号" align="center"></el-table-column>
                <el-table-column prop="name" label="客户名称"></el-table-column>
                <el-table-column prop="phone" label="电话"></el-table-column>
                <el-table-column prop="address" label="地址"></el-table-column>
                <el-table-column prop="city" label="城市"></el-table-column>
                <el-table-column prop="state" label="洲"></el-table-column>
                <el-table-column prop="country" label="国家"></el-table-column>
                <el-table-column prop="creditRating" label="信誉等级"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="this.form.id ? '编辑' : '新增'" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="员工编号" v-if="this.form.id">
                    <el-input v-model="form.id" readonly></el-input>
                </el-form-item>
                <el-form-item label="客户名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="客户电话">
                    <el-input v-model="form.phone" > </el-input>
                </el-form-item>
                <el-form-item label="客户地址">
                    <el-input v-model="form.address" > </el-input>
                </el-form-item>
                <el-form-item label="城市">
                    <el-input v-model="form.city" > </el-input>
                </el-form-item>
                <el-form-item label="洲">
                    <el-input v-model="form.state" > </el-input>
                </el-form-item>
                <el-form-item label="国家">
                    <el-input v-model="form.country" > </el-input>
                </el-form-item>
                <el-form-item label="信誉等级">
                    <el-input v-model="form.creditRating" > </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getDeptByCondition, saveOrUpdateDept } from '../api/dept';
import { empByPage } from '../api/emp';
import { titleByPage } from "../api/title"
import { findCusByPage,saveOrUpdateCus,deleteCusById,deleteCusByBatch,findCusByCondition } from "../api/customer"
import RegionList from '../components/common/RegionList.vue';
import RegionDeptList from '../components/common/RegionDeptList.vue';

export default {
    name: 'Customer',
    components: {
        RegionList,
        RegionDeptList
    },
    data() {
        //定义和当前vue组件绑定的数据模型。
        return {
            query: {
                // name: '',
                pageNum: 1,
                pageSize: 5
            },
            titles:[],
            dateRange: [],
            depts: [],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        //生命周期函数
        this.getData();
    },
    watch: {
        dateRange(val) {
            console.log('val', val);
            if (val) {
                this.query.startDate = val[0];
                this.query.endDate = val[1];
            } else {
                this.query.startDate = '';
                this.query.endDate = '';
            }
        }
    },
    methods: {
        changeQueryRegionId(val) {
            this.query.regionId = val;
        },
        //定义和当前vue组件绑定的方法。
        getData() {
            findCusByPage(this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        getCondition() {
            findCusByCondition(this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNum', 1);
            this.getCondition();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteCusById(row.id).then((resp) => {
                        this.$message.success(resp.data);
                        this.tableData.splice(index,1);
                        this.pageTotal--;
                        this.getData();
                    });
                    // this.$message.success('删除成功');
                    // this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log('-------选项变化------', this.multipleSelection);
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            if (length <= 0) {
                this.$message.error('请至少选择一项进行删除！');
                return; //结束
            }
            //发送ajax请求，进行批量删除操作。
            let ids = [];
            let str = [];
            for (let i = 0; i < length; i++) {
                ids.push(this.multipleSelection[i].id);
                str.push(this.multipleSelection[i].name);
            }
            console.log('ids === ', ids);
            this.$confirm('是否要删除区域【' + str + '】', '提示', {
                type: 'warning'
            }).then(() => {
                deleteCusByBatch(ids).then((resp) => {
                    this.$message.success(resp.data);
                    this.getData(); //刷新页面
                });
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log("员工信息row",row)
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            console.log('form', this.form);
            saveOrUpdateCus(this.form)
                .then((resp) => {
                    this.$message.success(resp.data);
                    //关闭对话框，
                    this.editVisible = false;
                    this.getData();
                    //发送网络请求，请求后台进行保存或更新的服务
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNum', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 5px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>